﻿using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Xml;
using GeneticAlgorithm;

namespace PSatSimCom
{
    public class PSatSimFileBuilder
    {
        public static void CreateXml(List<Configuration> configurations, List<string> traces, string filename)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            var doc = new XmlDocument();
            XmlElement mainNode = doc.CreateElement("psatsim");
            mainNode = (XmlElement)doc.AppendChild(mainNode);

            foreach (var trace in traces)
            {
                foreach (var configuration in configurations)
                {
                    XmlElement configNode = doc.CreateElement("config");
                    configNode.SetAttribute("name", trace + "_sim_" + configuration.Name);
                    configNode.SetAttribute("number", 0.ToString("D"));
                    mainNode.AppendChild(configNode);

                    XmlElement general = doc.CreateElement("general");
                    general.SetAttribute("superscalar", configuration.GetString("superscalar"));
                    general.SetAttribute("rename", configuration.GetString("rename"));
                    general.SetAttribute("reorder", configuration.GetString("reorder"));
                    general.SetAttribute("rsb_architecture", configuration.GetString("rsb_architecture"));
                    general.SetAttribute("rs_per_rsb", configuration.GetString("rs_per_rsb"));
                    general.SetAttribute("speculative", configuration.GetString("speculative"));
                    general.SetAttribute("speculation_accuracy", configuration.GetString("speculation_accuracy"));
                    general.SetAttribute("separate_dispatch", configuration.GetString("separate_dispatch"));
                    general.SetAttribute("seed", configuration.GetString("seed"));
                    general.SetAttribute("trace", trace);
                    general.SetAttribute("output", "");
                    general.SetAttribute("vdd", configuration.GetString("vdd"));
                    general.SetAttribute("frequency", configuration.GetString("frequency"));

                    configNode.AppendChild(general);

                    XmlElement execution = doc.CreateElement("execution");
                    execution.SetAttribute("architecture", configuration.GetString("architecture"));
                    execution.SetAttribute("integer", configuration.GetString("integer"));
                    execution.SetAttribute("floating", configuration.GetString("floating"));
                    execution.SetAttribute("branch", configuration.GetString("branch"));
                    execution.SetAttribute("memory", configuration.GetString("memory"));

                    configNode.AppendChild(execution);

                    XmlElement memory = doc.CreateElement("memory");
                    memory.SetAttribute("architecture", configuration.GetString("mem_architecture"));

                    configNode.AppendChild(memory);

                    XmlElement l1_code = doc.CreateElement("l1_code");
                    l1_code.SetAttribute("hitrate", configuration.GetString("l1Code_hitrate"));
                    l1_code.SetAttribute("latency", configuration.GetString("l1_latency"));

                    memory.AppendChild(l1_code);

                    XmlElement l1_data = doc.CreateElement("l1_data");
                    l1_data.SetAttribute("hitrate", configuration.GetString("l1Data_hitrate"));
                    l1_data.SetAttribute("latency", configuration.GetString("l1_latency"));

                    memory.AppendChild(l1_data);

                    XmlElement l2 = doc.CreateElement("l2");
                    l2.SetAttribute("hitrate", configuration.GetString("l2_hitrate"));
                    l2.SetAttribute("latency", configuration.GetString("l2_latency"));

                    memory.AppendChild(l2);

                    XmlElement system = doc.CreateElement("system");
                    system.SetAttribute("latency", configuration.GetString("sys_latency"));

                    memory.AppendChild(system);


                }
            }

            var writer = new StreamWriter(filename);
            doc.Save(writer);
            writer.Close();

        }
    }
}
